<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends AdminController {
    public function index(){
        $o_status = '1,2,3,4,5';
        $pageArr = $this->getPageOrders($o_status);
        $page = c_page($pageArr['count'],'个订单',5);

        $wh['status']  = array('in',$o_status); //订单状态
        $wh['order_id']=array('in',implode(',',$pageArr['orders'])); //订单列表
        $order = M('Order')
            ->alias('o')
            ->field('o.pay_type,o.status,o.u_time,o.c_time,o.order_id,o.id,o.goods_id,o.shop_price,o.attr,o.color,o.num,g.title,g.img,g.shop_price as goods_shop_price,g.attr as goods_attr')
            ->where($wh)
            ->join('__GOODS__ as g on g.id = o.goods_id','left')
            ->order('o.u_time desc')
            ->select();
        $new_order = $this->arrangeOrder($order);
//        var_dump($new_order);
//        exit;
//        var_dump($page);
//        exit;
//        var_dump($new_order);
//        exit;

//        if(IS_AJAX){
//            $json['data'] = $new_order;
//            $json['page'] = $page;
//            $this->ajaxReturn($json);
//        }else{
            $this->assign('title','订单列表');//标题
            $this->assign('data',$new_order);// 赋值数据集
            $this->assign('page',$page);// 赋值分页输出
            $this->display();
//        }

    }

    /**
     * 修改状态
     * @param int $status、
     * @param int $order_id
     */
    public function change_status($status = 0,$order_id = 0){
        if(IS_POST){
            if(!$status || !$order_id){
                $this->error('参数错误');
            }
            $wh['order_id'] = I('post.order_id');
            $aaa = I('post.order_id');
            $data['status']= $status;
            $data['u_time']=time();
            $res = M('Order')->data($data)->where($wh)->save();
            if($res !== false){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');

            }
        }
    }

    /**
     * @param $order  订单列表
     * @return array  整理后的订单列表
     */
    private function arrangeOrder($order){
        if(!$order){
            return $order;
        }
        foreach ($order as $v){
            $arr[] = $v['order_id'];
        }
        $uni_arr = array_unique($arr);
        $arr = array();
        foreach ($uni_arr as $v){
            $arr[$v] = array();
        }
        foreach ($order as $v){
            $one = M('user')->where(array('order_id'=>$v))->find();

            if($one){
                $v['buyer'] = $one['username'];
                array_push($arr[$v['order_id']],$v);
            }
        }

        return $arr;
    }

    protected function getPageOrders($o_status){
        //最多5个订单一页
        $curPage = I('get.p',1);
        $orders = M('Order')
            ->distinct(true)
            ->field('order_id')
            ->where(array('status'=>array('in',$o_status)))
            ->order('u_time desc')
            ->select();
//        var_dump(count($orders));
        foreach ($orders as $v){
            if($v['order_id']){
                $arr[] = $v['order_id'];
            }
        }
        $temp  = array_unique($arr);
        $count = count($temp);
        $j = 0;
        $orders = array();
        foreach ($temp as $v){
            if($j >= 5 *$curPage){
                break;
            }else if( $j >= 5 * ($curPage-1)){
                $orders[] = $v;
            }
            $j++;
        }

        $res=array(
            'count'=>$count,
            'orders'=>$orders,
        );
        return $res;
    }

    public function detail($id = 0){
        if($id < 1){
            $this->error("非法操作");
        }
        $one  = M('User')
            ->alias('a')
            ->where(array('a.id'=>I('get.id')))
            ->field('a.*,p.city as p_city,p.prov as p_prov,p.dist as p_dist,p.address as p_address , u.id as u_name')
//            ->join('__ADDRESS__ as p on a.address_id = p.id','left')
            ->join(array('LEFT JOIN __ADDRESS__ as p  ON a.address_id = p.id','LEFT JOIN __USER__  as u ON a.uesr_id = u.id'))
            ->select();

//        var_dump($one);
        exit;
        if($one){
            $this->ajaxReturn($one[0]);
        }else{
            $this->ajaxReturn(0);
        }
    }

    public function delete($id = 0){
        if($id < 1){
            $this->error('参数错误,至少选择一条数据');
        }


        if(!is_array($id)){
            $wh['order_id'] = $id;
            $res = M('Order')->where($wh)->delete();
        }else{
            $wh['order_id'] = array('in',implode(',', $id));
            $res = M('Order')->where($wh)->delete();
        }
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败：',U('index'));
        }
    }
}
?>